package com.imooc.coupon.controller;

import com.imooc.coupon.entity.Coupon;
import com.imooc.coupon.exception.CouponException;
import com.imooc.coupon.service.IUserService;
import com.imooc.coupon.vo.AcquireTemplateRequest;
import com.imooc.coupon.vo.CouponTemplateSDK;
import com.imooc.coupon.vo.SettlementInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h1>分发服务 Controller</h1>
 */
@Slf4j
@RestController
public class DisTributionController {

    @Autowired
    IUserService userService;

    /**
     * <h2>根据用户 id 和状态查询优惠券记录</h2>
     * 127.0.0.1:7002/coupon-distribution/distribution/findCouponsByStatus
     * 127.0.0.1:9000/imooc/coupon-distribution/distribution/findCouponsByStatus
     * @param userId 用户 id
     * @param status 优惠券状态
     * @return {@link Coupon}s
     * */
    @GetMapping("/distribution/findCouponsByStatus")
    List<Coupon> findCouponsByStatus(@RequestParam("userId")Long userId, @RequestParam("status")Integer status)
            throws CouponException{
        return userService.findCouponsByStatus(userId,status);
    }

    /**
     * <h2>根据用户 id 查找当前可以领取的优惠券模板</h2>
     * @param userId 用户 id
     * @return {@link CouponTemplateSDK}s
     * */
    @GetMapping("/distribution/findAvailableTemplate")
    List<CouponTemplateSDK> findAvailableTemplate(@RequestParam("userId")Long userId)
            throws CouponException{
        return userService.findAvailableTemplate(userId);
    }

    /**
     * <h2>用户领取优惠券</h2>
     * @param request {@link AcquireTemplateRequest}
     * @return {@link Coupon}
     * */
    @PostMapping("/distribution/acquireTemplate")
    Coupon acquireTemplate(@RequestBody AcquireTemplateRequest request)
            throws CouponException{
        return userService.acquireTemplate(request);
    }

    /**
     * <h2>结算(核销)优惠券</h2>
     * @param info {@link SettlementInfo}
     * @return {@link SettlementInfo}
     * */
    @PostMapping("/distribution/settlement")
    SettlementInfo settlement(@RequestBody SettlementInfo info) throws CouponException{
        return userService.settlement(info);
    }
}
